<?php

namespace app\modules\sign\controllers;

use Yii;
use app\modules\cruise\models\Customer;
use yii\data\Pagination;
use yii\web\Controller;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use app\modules\cruise\models\Member;

class SignController extends Controller
{ 

    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                ],
            ],
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'actions' => ['index', 'sign', 'statis', 'detail','cancel','confirm','cancel-list'],
                        'allow' => true,
                        'matchCallback' => function ($rule, $action) {
                            $member  = Yii::$app->session->get('member');
                            return empty($member) ?  $this->redirect(['member/login']) : true;
                        }
                    ],
                ],
            ],//access
        ];
    }

	  //人员名单
    public function actionIndex()
    {

      $member = \Yii::$app->session->get('member');
      $member_model = Member::find()->where(['phone'=>$member['phone'],'status'=>1])->one();
      $role = $member_model['role']; 

    	$data = yii::$app->request->get();
      $group_tag = isset($data['type_v']) ? $data['type_v'] : '';
      $group_arr = $group_tag_arr = [];
      $all_group = Customer::find()->where(['status'=>1])->groupBy('group')->all();
      if($all_group){
         foreach($all_group as $c){
           if($c->group){
             $group_arr[$c->group] = $c->group;
           }
         } 
      }
      sort($group_arr);//排序

      if($role == 2){
         $group_arr = json_decode($member_model->group_num);
      }
      

    	$query = Customer::find()->where(['status'=>1]);
    	if(!empty($data['type_v']) && !empty($data['type'])){
    		if($data['type'] == 1){ //手机号查询
    			$query->andFilterWhere(['like', 'phone',$data['type_v']]);
    		}else if($data['type'] == 2){ //护照号查询
    			$query->andFilterWhere(['like', 'passport',$data['type_v']]);
    		}else if($data['type'] == 3){ //团号查询

          if($group_tag){
              $group_tag_arr = explode(',',$group_tag);
              $group_tag_arr = array_filter($group_tag_arr);
          }
          $query->andWhere(['in', 'group',$group_tag_arr]);

        }else if($data['type'] == 4){
          $query->andFilterWhere(['like', 'name',$data['type_v']]);
        }
    	}

      if($role == 2){
         $group_arr = array_filter($group_arr);
         $query->andWhere(['in', 'group',$group_arr]);
      }

      
    	$pages = new Pagination(['totalCount' =>$query->count(), 'pageSize' => '20']);

      $model = $query->offset($pages->offset)->limit($pages->limit)->all();

      return $this->render('index',['params'=>$data,'model' => $model,
             'pages' => $pages,'group_arr'=>$group_arr,'group_tag'=>$group_tag,'group_tag_arr'=>$group_tag_arr]);
    }

    //取消名单
    public function actionCancelList()
    {
      $member = \Yii::$app->session->get('member');
      $member_model = Member::find()->where(['phone'=>$member['phone'],'status'=>1])->one();
      $role = $member_model['role']; 
      if($role != 3){
         return false;
      }
      $data = yii::$app->request->get();
      $query = Customer::find()->where(['status'=>1])->andFilterWhere(['>=','cancel','2']);
      if(!empty($data['type_v']) && !empty($data['type'])){
        if($data['type'] == 1){ //手机号查询
          $query->andFilterWhere(['like', 'phone',$data['type_v']]);
        }else if($data['type'] == 2){ //护照号查询
          $query->andFilterWhere(['like', 'passport',$data['type_v']]);
        }else if($data['type'] == 3){ //团号查询
          $query->andFilterWhere(['like', 'group',$data['type_v']]);
        }else if($data['type'] == 4){
          $query->andFilterWhere(['like', 'name',$data['type_v']]);
        }
      }

      $pages = new Pagination(['totalCount' =>$query->count(), 'pageSize' => '20']);

      $model = $query->offset($pages->offset)->limit($pages->limit)->all();

      return $this->render('cancel',['params'=>$data,'model' => $model,
             'pages' => $pages]);

    }
    //取消
    public function actionCancel(){
       $data = yii::$app->request->get();
       $id = isset($data['id']) ? $data['id'] : 0;
       $type = isset($data['type']) ? $data['type'] : 0;

       if($id){
          $model = Customer::findOne($id);
          if($model){
              if($type == 1){  //申请取消
                $model->cancel = 2;
                if($model->save()){
                  echo 1;exit();
                }else{
                  echo 0;exit();
                }
              }else if($type == 2){ //撤回取消
                $model->cancel = 1;
                if($model->save()){
                  echo 1;exit();
                }else{
                  echo 0;exit();
                }
             }
              
          }else{
             echo 0;exit();
          }
       }else{
         echo 0;exit();
       }  
    }

    //确认驳回取消
    public function actionConfirm(){
       $data = yii::$app->request->get();
       $id = isset($data['id']) ? $data['id'] : 0;
       $type = isset($data['type']) ? $data['type'] : 0;

       if($id){
          $model = Customer::findOne($id);
          if($model){
              if($type == 1){ //确认
                $model->cancel = 3;
                if($model->save()){
                  echo 1;exit();
                }else{
                  echo 0;exit();
                }
              }else if($type == 2){ //取消
                $model->cancel = 1;
                if($model->save()){
                  echo 1;exit();
                }else{
                  echo 0;exit();
                }
             }else if($type == 3){
                $model->cancel = 1;
                if($model->save()){
                  echo 1;exit();
                }else{
                  echo 0;exit();
                }
             }
          }else{
             echo 0;exit();
          }
       }else{
         echo 0;exit();
       }  
    }


    //签到
    public function actionSign(){
       $data = yii::$app->request->get();
       $id = isset($data['id']) ? $data['id'] : 0;
       $type = isset($data['type']) ? $data['type'] : 0;
       if($id){
       	  $model = Customer::findOne($id);
       	  if($model){
       	  	 if($type == 1){ //签到
       	  	 	$model->sign = 1;
       	  	 	if($model->save()){
       	  	 		echo 1;exit();
       	  	 	}else{
       	  	 		echo 0;exit();
       	  	 	}
       	  	 }else if($type == 2){ //取消签到
       	  	 	$model->sign = 0;
       	  	 	if($model->save()){
       	  	 		echo 1;exit();
       	  	 	}else{
       	  	 		echo 0;exit();
       	  	 	}
       	  	 }
       	  	 
       	  }else{
       	  	 echo 0;exit();
       	  }
       }else{
       	 echo 0;exit();
       }	

    }

    //统计
    public function actionStatis()
    {   
      $member = \Yii::$app->session->get('member');
      $member_model = Member::find()->where(['phone'=>$member['phone'],'status'=>1])->one();
      $role = $member_model['role']; 

      if($role == 2){ //领队
        $data = yii::$app->request->post();
        $group_arr = [];
        $group_tag = isset($data['group_tag']) ? $data['group_tag'] : '';
        $g_n = json_decode($member_model->group_num);
        if(is_array($g_n)){
          $g_n = implode(",",$g_n);
        }
        if($group_tag){
          $group_tag_arr = explode(',',$group_tag);
          $group_tag_arr = array_filter($group_tag_arr);
        }else{
          $group_tag = $g_n;
          $group_tag_arr = json_decode($member_model->group_num);
          $group_tag_arr = array_filter($group_tag_arr);
        }
        $group_arr = json_decode($member_model->group_num);
       
        $num = Customer::find()->where(['status'=>1,'sign'=>1])->andWhere(['<','cancel',3])->andWhere(['in','group',$group_tag_arr])->count();
        $num2 = Customer::find()->where(['status'=>1,'sign'=>0])->andWhere(['<','cancel',3])->andWhere(['in','group',$group_tag_arr])->count(); 
        $num3 = Customer::find()->where(['status'=>1])->andWhere(['=','cancel',3])->andWhere(['in','group',$group_tag_arr])->count();
      }else{
          //分组获取所有团号
          $group_arr = [];
          $model = Customer::find()->where(['status'=>1])->groupBy('group')->all();
          if($model){
             foreach($model as $c){
               if($c->group){
                 $group_arr[$c->group] = $c->group;
               }
             } 
          }
          sort($group_arr);//排序

          $data = yii::$app->request->post();
          $num = $num2 = $num3= 0;
          $group_tag = '';
          $group_tag_arr = [];
          if($data){
             $group_tag = isset($data['group_tag']) ? $data['group_tag'] : '';
             if($group_tag){
                $group_tag_arr = explode(',',$group_tag);
                $group_tag_arr = array_filter($group_tag_arr);
                $num = Customer::find()->where(['status'=>1,'sign'=>1])->andWhere(['<','cancel',3])->andWhere(['in','group',$group_tag_arr])->count();
                $num2 = Customer::find()->where(['status'=>1,'sign'=>0])->andWhere(['<','cancel',3])->andWhere(['in','group',$group_tag_arr])->count(); 
                $num3 = Customer::find()->where(['status'=>1])->andWhere(['=','cancel',3])->andWhere(['in','group',$group_tag_arr])->count();
             }
          }else{
            $num = Customer::find()->where(['status'=>1,'sign'=>1])->andWhere(['<','cancel',3])->count();
            $num2 = Customer::find()->where(['status'=>1,'sign'=>0])->andWhere(['<','cancel',3])->count(); 
            $num3 = Customer::find()->where(['status'=>1])->andWhere(['=','cancel',3])->count();
          }
      }
        
        
        return $this->render('statis',['num'=>$num,'num2'=>$num2,'num3'=>$num3,'group_arr'=>$group_arr,'group_tag'=>$group_tag,'group_tag_arr'=>$group_tag_arr]);
    }

    //详情
    public function actionDetail()
    {

      $data = yii::$app->request->get();
      $group_tag = $sort ='';
      $group_tag_arr = [];
      if($data){
        $sort = isset($data['sort']) ? $data['sort'] : 'group'; 
        $group_tag = isset($data['group_tag']) ? $data['group_tag'] : '';
      }

      $query = Customer::find()->where(['status'=>1]);
      if($group_tag){
         $group_tag_arr = explode(',',$group_tag);
         $group_tag_arr = array_filter($group_tag_arr);
         $query->andWhere(['in','group',$group_tag_arr]);
      }
      if($sort == 'rank'){
        $query->orderBy('rank');
      }else{
        $query->orderBy('group');
      }
      
      $pages = new Pagination(['totalCount' =>$query->count(), 'pageSize' => '20']);
      $model = $query->offset($pages->offset)->limit($pages->limit)->all();
      return $this->render('detail',['model'=>$model,'sort'=>$sort,'group_tag'=>$group_tag,'pages'=>$pages]);
    }
}
